package ch.fusun.baron.basic.command;

import ch.fusun.baron.core.command.GameCommand;
import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.player.Dynasty;
import ch.fusun.baron.player.api.PlayerService;
import ch.fusun.baron.treasury.api.TreasuryService;
import ch.fusun.baron.turn.UserTurnService;

/**
 * Abstract class for classes that need money to be executed
 */
public abstract class MoneyTurnCommand extends GameCommand {
	@Inject
	private transient UserTurnService userTurnService;
	@Inject
	private transient TreasuryService treasuryService;
	@Inject
	private transient PlayerService playerService;

	@Override
	public boolean isAllowed() {
		return (treasuryService.getMoney(getDynasty()) >= getMoneyCost())
				&& userTurnService.getTurnPoints(playerService
						.getUser(getDynasty())) >= getTurnCost()
				&& isAllowedImpl();
	}

	/**
	 * @return is it allowed?
	 */
	protected abstract boolean isAllowedImpl();

	/**
	 * @return how many turns does it cost?
	 */
	protected abstract int getTurnCost();

	/**
	 * @return the dynasty
	 */
	protected abstract Dynasty getDynasty();

	/**
	 * @return how much money does it cost?
	 */
	protected abstract int getMoneyCost();

	@Override
	public void execute() {
		treasuryService.removeMoney(getDynasty(), getMoneyCost());
		userTurnService.decreaseTurnPoints(playerService.getUser(getDynasty()),
				getTurnCost());
		executeImpl();
	}

	/**
	 * Execute the command
	 */
	protected abstract void executeImpl();

}
